package cn.wolfocde.wolf2w.controller;

import cn.wolfocde.wolf2w.annotation.RequireLoginAnnotation;
import cn.wolfocde.wolf2w.domain.UserInfo;
import cn.wolfocde.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfocde.wolf2w.service.IUserInfoService;
import cn.wolfocde.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
@RequestMapping("/users")
public class UserinfoController {
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private IUserInfoService userInfoService;

    //获取配置文件里面的发到网关公司的信息()
    @Value("${sms.url}")
    private  String smsUrl;
    @Value("${sms.appkey}")
    private  String smsAppkey;

//检查账号是否存在
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone) {
//        前缀+列+操作符 (is省略)
       boolean ref=userInfoService.findByPhone(phone);
        return !ref ;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone) {
        userInfoRedisService.sendVerifyCode(phone,smsUrl,smsAppkey);
        return JsonResult.success();
    }

    ////第二次请求访问
    // 获取redis的当前登录用户对象
    //给前端页面做头像显示
    @GetMapping("/currentUser")
    public Object getCurrentUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        UserInfo user = userInfoRedisService.getCurrentUserByToken(token);
        return JsonResult.success(user);
    }
    ////获取某个用户对象
    //贴了这个注解,表示要登录才能访问这个映射方法
     @RequireLoginAnnotation
    @GetMapping("/get")
    public Object get(String id) {
        UserInfo user=null;
        if (StringUtils.hasLength(id)) {
             user = userInfoService.get(id);
            return JsonResult.success(user);
        }
        return JsonResult.success(user);

    }

    //用户注册
    @PostMapping("/regist")
    public Object regist(String nickname,
     String phone ,String password,String rpassword ,String verifyCode) {
            userInfoService.regist( nickname,
                    phone , password, rpassword , verifyCode);


        return JsonResult.success();
    }

    //用户登录
    @PostMapping("/login")
    public Object login(String username ,String password) {
        //用户对象能够返回到控制器,说明密码和手机号是正确的
        //表示登录成功了,就可以生成token,拼接好key,同时存入redis
         UserInfo user = userInfoService.login(username, password);
        //生成token,并且最终返回到前端页面
        //拼接好key,同时存入redis
       String token=userInfoRedisService.setToken(user);
        HashMap<String, Object> map = new HashMap<>();
        map.put("user",user);
        map.put("token",token);
        return JsonResult.success(map);
    }
}
